package com.antifly.common.constant;


import java.util.Map;

/**
 * 统一返回数据格式
 *
 * @author JInit
 * @date 2019/05/21
 */
public class ResponseBean {
    /**
     * 返回码
     */
    private String code = "0";
    /**
     * 返回说明
     */
    private String msg = "success";
    /**
     * 业务数据
     */
    private Object data;

    /**
     * 获取返回码
     *
     * @return
     */
    public String getCode() {
        return code;
    }

    /**
     * 设置返回码
     *
     * @return
     */
    public ResponseBean setCode(String code) {
        this.code = code;
        return this;
    }

    /**
     * 获取返回信息
     *
     * @return
     */
    public String getMsg() {
        return msg;
    }

    /**
     * 设置返回信息
     *
     * @param msg
     * @return
     */
    public ResponseBean setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    /**
     * 获取数据
     *
     * @return
     */
    public Object getData() {
        return data;
    }

    /**
     * 设置数据
     *
     * @param data
     * @return
     */
    public ResponseBean setData(Object data) {
        this.data = data;
        return this;
    }

    public ResponseBean(Object data) {
        this.data = data;
    }

    public ResponseBean(ResponseCode responseCode) {
        this.code = responseCode.getCode();
        this.msg = responseCode.getMsg();
    }

    /**
     * 设置返回状态
     *
     * @param responseCode
     * @return
     */
    public ResponseBean setResponseCode(MobileResponseCode responseCode) {
        this.setCode(responseCode.getCode());
        this.setMsg(responseCode.getMsg());
        return this;
    }

    public ResponseBean(String code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ResponseBean(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    /**
     * 设置ResponseBean对象属性
     *
     * @param parameterMap 设置参数
     * @return
     */
    public ResponseBean setResponse(Map<String, Object> parameterMap) {
        for (Map.Entry<String, Object> entry : parameterMap.entrySet()) {
            Object key = entry.getKey();
            Object value = entry.getValue();
            this.setCode("code".equals(key) ? String.valueOf(value) : null);
            this.setMsg("msg".equals(key) ? String.valueOf(value) : null);
        }
        this.setData(parameterMap.get("data"));
        return this;
    }
}
